﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ipboxshiyan
{
    public partial class ipBox : UserControl
    {
        public ipBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取ip地址
        /// </summary>
        /// <returns></returns>
        public string GetIpAddress()
        {
            string ipValue = string.Empty;
            ipValue = ipValue + tbip1.Text.Trim() + ".";
            ipValue = ipValue + tbip2.Text.Trim() + ".";
            ipValue = ipValue + tbip3.Text.Trim() + ".";
            ipValue = ipValue + tbip4.Text.Trim();

            return ipValue;
        }

        public bool SetIpAddress(string ipValue)
        {
            tbip1.Text = string.Empty;
            tbip2.Text = string.Empty;
            tbip3.Text = string.Empty;
            tbip4.Text = string.Empty;

            //判断ip地址是否合法
            if(ipValue.Length<7)
            {
                return false;
            }

            int index = 0;

            for (int i = 0; i < 3;i++ )
            {
                index = ipValue.IndexOf(".",index+1);

                if (index == -1)
                {
                    return false;
                }
            }

            //解析ip地址
            string[] ipValues=new string[4];

            ipValues = ipValue.Split('.');
            try
            {
                tbip1.Text = Convert.ToInt16(ipValues[0]).ToString();
                tbip2.Text = Convert.ToInt16(ipValues[1]).ToString();
                tbip3.Text = Convert.ToInt16(ipValues[2]).ToString();
                tbip4.Text = Convert.ToInt16(ipValues[3]).ToString();

            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 控制textbox只能输入数字
        /// </summary>
        /// <param name="textBox"></param>>
        /// <param name="e"></param>
        private void InputRegular(KeyPressEventArgs e)
        {
            //只能输入数字，退格键
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
            {
            }
            else
            {
                e.Handled = true;
            }
        }

        private void tbip1_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //接受的字符为符号或当前textbox数字大于3个时，则跳跃到下一个textbox。
            if (e.KeyChar == 46||this.tbip1.Text.Length>=2)
            {
                tbip2.Focus();
            }
        }

        private void tbip2_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //接受的字符为符号“.”或当前textbox数字大于3个时，则跳跃到下一个textbox。
            if (e.KeyChar == 46 || this.tbip2.Text.Length >=2)
            {
                tbip3.Focus();
            }
            //接受的字符为退格键且当前textbox没有数据，则回跳至前一个textbox
            else if(e.KeyChar==8&&this.tbip2.Text.Length==0)
            {
                tbip1.Focus();
            }
        }

        private void tbip3_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //接受的字符为符号“.”或当前textbox数字大于3个时，则跳跃到下一个textbox。
            if (e.KeyChar == 46 || this.tbip3.Text.Length >= 2)
            {
                tbip4.Focus();
            }
            //接受的字符为退格键且当前textbox没有数据，则回跳至前一个textbox
            else if (e.KeyChar == 8 && this.tbip3.Text.Length == 0)
            {
                tbip2.Focus();
            }
        }

        private void tbip4_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //当前textbox数字大于3个时，则限制输入。
            if (this.tbip4.Text.Length >= 2)
            {
                //截取字符
                this.tbip4.Text = this.tbip4.Text.Substring(0,2);
                tbip4.SelectAll();
                tbip4.SelectionStart = tbip4.Text.Length;
                tbip4.Focus();
            }
            //接受的字符为退格键且当前textbox没有数据，则回跳至前一个textbox
            else if (e.KeyChar == 8 && this.tbip4.Text.Length == 0)
            {
                tbip3.Focus();
            }
        }

        /// <summary>
        /// 控制最大值不能超过255
        /// </summary>
        /// <param name="textBox"></param>
        private void InputRange(System.Windows.Forms.TextBox textBox)
        {
            //控制最大值不能超过255
            if(textBox.Text.Length==0)
            {
                return;
            }

            if(Convert.ToInt16(textBox.Text)>255)
            {
                MessageBox.Show(textBox.Text+"不是0-255之间的数");
                textBox.Text = "255";
}
        }

        private void tbip1_TextChanged(object sender, EventArgs e)
        {
            InputRange(tbip1);
        }

        private void tbip2_TextChanged(object sender, EventArgs e)
        {
            InputRange(tbip2);
        }

        private void tbip3_TextChanged(object sender, EventArgs e)
        {
            InputRange(tbip3);
        }

        private void tbip4_TextChanged(object sender, EventArgs e)
        {
            InputRange(tbip4);
        }

        private void ipBox_Load(object sender, EventArgs e)
        {
            tbip1.Text = "";
            tbip2.Text = "";
            tbip3.Text = "";
            tbip4.Text = "";

        }
    }
}
